package com.hello.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.hello.mapper.ItemsMapper;
import com.hello.pojo.Items;
import com.hello.pojo.PageBean;
import com.hello.service.ItemsService;

@Component(value = "ItemsService")
@Service
public class ItemsServiceImpl implements ItemsService {

	@Autowired
	private ItemsMapper itemsMapper;

	@Override
	public Items findItemById(Integer id) {
		// TODO Auto-generated method stub
		return itemsMapper.selectById(id);
	}

	@Override
	public List<Items> list() {
		// TODO Auto-generated method stub
		return itemsMapper.selectItemsList();
	}

	@Override
	public void updateItems(Items items) {
		// TODO Auto-generated method stub
		itemsMapper.updateById(items);
	}

	@Override
	public void deleteItemsList(Integer id) {
		// TODO Auto-generated method stub
		itemsMapper.deleteItemsList(id);
	}

	@Override
	public int selectCount() {
		// TODO Auto-generated method stub
		return itemsMapper.selectCount();
	}

	@Override
	public PageBean<Items> findByPage(int currentPage) {
		// TODO Auto-generated method stub
		HashMap<String, Object> map = new HashMap<String, Object>();
		PageBean<Items> pageBean = new PageBean<Items>();

		// 封装当前页数
		pageBean.setCurrPage(currentPage);

		// 每页显示的数据
		int pageSize = 2;
		pageBean.setPageSize(pageSize);

		// 封装总记录数
		int totalCount = itemsMapper.selectCount();
		pageBean.setTotalCount(totalCount);

		// 封装总页数
		double tc = totalCount;
		Double num = Math.ceil(tc / pageSize);
		pageBean.setTotalPage(num.intValue());

		map.put("start", (currentPage - 1) * pageSize);
		map.put("size", pageBean.getPageSize());

		// 封装每页显示的数据
		List<Items> lists = itemsMapper.findByPage(map);
		pageBean.setLists(lists);
		return pageBean;
	}

}
